#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import sys
sys.path.append("..")
import fileutils

cwd = os.getcwd()
root_dir = os.path.join(cwd, "..", "..")
res_dir = os.path.join(root_dir, "cocosstudio", "resource")

template_tps = "template.tps"
template_bat = "template.bat"

def gentps(dir, tps, bat):
    dirname = os.path.dirname(dir)    
    basename = os.path.basename(dir).replace("_PList.Dir", "").replace(".", "")
    #print(basename)
    tpsname = os.path.join(dirname, basename)  + ".tps"
    batname = os.path.join(dirname, basename)  + ".bat"
    #print (tpsname)

    tps = tps.replace("{template}", basename)
    bat = bat.replace("{template}", basename)

    if not os.path.isfile(tpsname):
        writefile(tpsname, tps)

    if not os.path.isfile(batname):
        writefile(batname, bat)
    pass

def readbat():
    ret = None
    f = open(template_bat, "r")
    ret = f.read()
    f.close()
    return ret

def readtps():
    ret = None
    f = open(template_tps, "r")
    ret = f.read()
    f.close()
    return ret

def writefile(file, content):
    f = open(file, "w")
    f.write(content)
    f.close()

#################################################

if __name__ == '__main__':
    tps = readtps()
    bat = readbat()
    #print (template)
    def __walk__(dir):        
        if "_PList.Dir" in dir:
            gentps(dir, tps, bat)
        pass
    fileutils.walkdir(res_dir, __walk__)
    pass